package com.ophiux.controller;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping
public class TestConroller {
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;


    /**
     * ServiceTask测试
     * Expression  配置  "${myServiceTask.hasRole(execution)}" 会先进入{@link com.ophiux.service.impl.MyServiceTask}
     */
    @GetMapping("/test1")
    public void index(){
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("processes/MyServiceTask.bpmn20.xml")
                .name("测试ServiceTask").deploy();


        String deploymentId = deployment.getId();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();

        Map<String, Object> map = new HashMap<>();
        map.put("day", 16);
        //通过流程定义ID启动一个流程实例
        //流程定义ID    hhl_task_01:1:2503
        //流程定义key   hhl_task_01
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId(),map);
        System.out.println("流程实例的ID： processInstance.getId() = " + processInstance.getId());
        System.out.println("流程名：processInstance.getName(() = " + processInstance.getName());
    }

    /**
     * 测试EmailTask
     */
    @GetMapping("/test2")
    public void test2(){
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("processes/EmailTask.bpmn20.xml")
                .name("测试EmailTask").deploy();

        Map<String, Object> variables = new HashMap<>();
        variables.put("from", "beyond_world@sina.cn");
        variables.put("to", "1414454875@qq.com");
        variables.put("subject", "Hello World");
        variables.put("text", "欢迎学习Activiti邮件发送功能！");

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("EmailTask",variables);
        System.out.println("processInstance.getId() = " + processInstance.getId());
        System.out.println("processInstance.getName() = " + processInstance.getName());
        System.out.println("processInstance.getDescription() = " + processInstance.getDescription());
    }


    /**
     * 子流程测试
     */
    @GetMapping("/test3")
    public void test3() throws IOException {
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("processes/sub.bpmn20.xml")
                .name("子流程测试").deploy();

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("sub");
        System.out.println("processInstance.getId() = " + processInstance.getId());
        System.out.println("processInstance.getName() = " + processInstance.getName());
        System.out.println("processInstance.getDescription() = " + processInstance.getDescription());
    }


}
